package com.xiaoyu.lanling.feature.generosity

import `in`.srain.cube.views.list.CubeRecyclerViewSimpleAdapter
import android.os.Bundle
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.data.UserData
import com.xiaoyu.base.event.AppEventBus
import com.xiaoyu.base.event.SimpleEventHandler
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.activity.base.AppCompatToolbarActivity
import com.xiaoyu.lanling.event.charming.LevelUpMeansItem
import com.xiaoyu.lanling.event.charming.viewholder.LevelUpMeansViewHolder
import com.xiaoyu.lanling.event.generosity.GenerosityLevelEvent
import com.xiaoyu.lanling.media.image.ImageViewUtil
import kotlinx.android.synthetic.main.activity_my_generosity_level.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MyGenerosityLevelActivity : AppCompatToolbarActivity() {
    val requestTag = Any()
    private lateinit var adapter: CubeRecyclerViewSimpleAdapter<LevelUpMeansItem>
    override fun onCreateSafelyAfterAppFinishInit(savedInstanceState: Bundle?) {
        super.onCreateSafelyAfterAppFinishInit(savedInstanceState)
        setLightStatusBar()
        setContentView(R.layout.activity_my_generosity_level)
        initView()
        initEvent()
        initData()
    }

    private fun initView() {
        initToolbar()
        title = AppContext.getString(R.string.generosity_title)
        adapter = CubeRecyclerViewSimpleAdapter<LevelUpMeansItem>()
        adapter.setViewHolderClass(0, null, LevelUpMeansViewHolder::class.java, 20)
        recyclerview.adapter = adapter
    }

    private fun initData() {
        GenerosityLevelData.getGenerosityLevelData(requestTag)
    }

    private fun initEvent() {
        AppEventBus.bindContainerAndHandler(this, object : SimpleEventHandler() {
            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: GenerosityLevelEvent) {
                showData(event)
            }
        })
    }

    private fun showData(event: GenerosityLevelEvent) {
        ImageViewUtil.loadAvatarRound(avatar, UserData.getInstance().user, 96)
        ImageViewUtil.loadImage(level_icon, event.iconLoadParam)
        generosity_curr_level.text = event.level
        generosity_next_level.text = event.nextLevel
        level_progress.progress = event.progress
        generosity_num.text = event.value.toString()
        generosity_level.text = event.level
        level_up_distance_remind.text = AppContext.getString(R.string.generosity_level_up_value, event.rest)
        adapter.list = event.list
        adapter.notifyDataSetChanged()
    }
}